<?php

namespace App\Resource\Admin;

use App\Model\Article;
use Hyperf\Resource\Json\JsonResource;

/**
 * @mixin Article
 */
class SimpleArticleResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array
     */
    public function toArray(): array
    {
        return [
            'id' => $this->id,
            'title' => $this->title,
            'slug' => $this->slug,
            'summary' => $this->summary,
            'content_type' => $this->content_type,
            'cover_image' => $this->cover_image,
            'status' => $this->status,
            'view_count' => $this->view_count,
            'like_count' => $this->like_count,
            'comment_count' => $this->comment_count,
            'favorite_count' => $this->favorite_count,
            'reading_time' => $this->reading_time,
            'published_at' => $this->published_at?->format('Y-m-d H:i:s'),
            'created_at' => $this->created_at?->format('Y-m-d H:i:s'),
            'updated_at' => $this->updated_at?->format('Y-m-d H:i:s'),

            'user' => $this->whenLoaded('user', fn () => [
                'id' => $this->user->id,
                'name' => $this->user->name,
            ])
        ];
    }
}
